//
//  TSCreateMsgTableView.m
//  chart
//
//  Created by xu on 15/7/31.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSCreateMsgTableView.h"
#import "TSMsgContentItemCell.h"
#import "TSMsgDatePickerTextFieldCell.h"
#import "TSPickerViewTextFieldCell.h"

@implementation TSCreateMsgTableView{
    NSArray *rowsArray;
}

@synthesize msgModel;

- (id)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMultipleSelectCompelete:) name:TSNotification_OnMultipleSelectCompelete object:nil];
        
        rowsArray = @[@"标题:", @"时间:", @"地点:", @"内容:", @"发送人:", @"接收人:", @"附件:", @"时限:"];
        
        self.dataSource = self;
        self.delegate = self;
        
        self.tableFooterView = [[UIView alloc] init];
    }
    
    return self;
}

- (void)onMultipleSelectCompelete:(NSNotification *)noti
{
    NSString *names = noti.object;
    NSIndexPath *path = [NSIndexPath indexPathForRow:5 inSection:0];
    TSMsgContentItemCell *cell = (TSMsgContentItemCell *)[self cellForRowAtIndexPath:path];
    [cell.contentTextField setText:names];
}

/* 是否属于picker cell */
- (CELL_TYTPE)getCellType:(NSString *)name
{
    NSArray *date_picker_array = @[@"时间:"];
    NSArray *picker_view_array = @[@"时限:"];
    
    for (NSString *picker_name in date_picker_array) {
        if ([picker_name isEqualToString:name]) {
            return DATE_PICKER_TEXT_FIELD_CELL;
        }
    }
    
    for (NSString *picker_name in picker_view_array) {
        if ([picker_name isEqualToString:name]) {
            return PICKER_VIEW_TEXT_FIELD_CELL;
        }
    }
    
    return DEFAULT_TEXT_FIELD_CELL;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return rowsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    NSString *title = [rowsArray objectAtIndex:indexPath.row];
    CELL_TYTPE cell_type = [self getCellType:title];
    
    if (rowsArray && rowsArray.count > indexPath.row) {
        switch (cell_type) {
            case DEFAULT_TEXT_FIELD_CELL:
            {
                static NSString *item_cell_identi = @"item_cell_identi";
                TSMsgContentItemCell *item_cell = [tableView dequeueReusableCellWithIdentifier:item_cell_identi];
                if (!item_cell) {
                    item_cell = [[TSMsgContentItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:item_cell_identi];
                    
                    [item_cell setTitleAndTextField:title Type:UITextFieldDefault];
                    [item_cell.contentTextField setText:[self getTextByTitle:title]];
                }
               
                
                if ([title isEqualToString:@"发送人:"]) {
                    [item_cell.contentTextField setText:[TSUserModel sharedInstance].userName];
                    [item_cell setTextFieldEditable:NO];
                }
                else if ([title isEqualToString:@"接收人:"]){
                    [item_cell setNotShowKeyboard:YES];
                }
                
                cell =item_cell;
            }
                break;
            case DATE_PICKER_TEXT_FIELD_CELL:
            {
                static NSString *picker_cell_identi = @"picker_cell";
                TSMsgDatePickerTextFieldCell *picker_cell = [tableView dequeueReusableCellWithIdentifier:picker_cell_identi];
                if (!picker_cell) {
                    picker_cell = [[TSMsgDatePickerTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:picker_cell_identi];
                    [picker_cell setTitle:title];
                    [picker_cell.contentTextField setText:[self getTextByTitle:title]];
                }
                
                cell = picker_cell;
            }
                break;
            case PICKER_VIEW_TEXT_FIELD_CELL:
            {
                static NSString *picker_cell_identi = @"picker_view_cell";
                TSPickerViewTextFieldCell *picker_view_cell = [tableView dequeueReusableCellWithIdentifier:picker_cell_identi];
                if (!picker_view_cell) {
                    picker_view_cell = [[TSPickerViewTextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:picker_cell_identi WithSourceArray:@[@"5分钟", @"10分钟", @"20分钟"]];
                    [picker_view_cell setTitle:title];
                    [picker_view_cell.contentTextField setText:[self getTextByTitle:title]];
                }
                
                cell = picker_view_cell;
            }
                break;
                
            default:
                break;
        }
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return CELL_HEIGHT;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [NOTIFICATIONCENTER postNotificationName:TSNotification_ResignFirstResponder object:nil];
}

- (UITableViewCell *)getCellByTitle:(NSString *)title
{
    UITableViewCell *cell = [self cellForRowAtIndexPath:[self getIndexPathByTitle:title]];
    
    return cell;
}

- (NSIndexPath *)getIndexPathByTitle:(NSString *)title
{
    if (rowsArray) {
        for (int i=0; i<rowsArray.count; i++) {
            if ([[rowsArray objectAtIndex:i] isEqualToString:title]) {
                NSIndexPath *index_path = [NSIndexPath indexPathForItem:i inSection:0];
                return index_path;
            }
        }
    }
    
    return nil;
}

- (NSString *)getTextByTitle:(NSString *)title
{
//    @[@"标题:", @"时间:", @"地点:", @"内容:", @"发送人:", @"接收人:", @"附件:", @"时限:"];
    NSString *result_text = @"";
    if (msgModel) {
        if ([title isEqualToString:@"标题:"]) {
            result_text = msgModel.title;
        }
        else if ([title isEqualToString:@"时间:"]) {
            result_text = [TSTools getDateByTime:msgModel.time];
        }
        else if ([title isEqualToString:@"地点:"]) {
            result_text = msgModel.place;
        }
        else if ([title isEqualToString:@"内容:"]) {
            result_text = msgModel.messageData;
        }
        else if ([title isEqualToString:@"发送人:"]) {
            result_text = msgModel.sender;
        }
        else if ([title isEqualToString:@"接收人:"]) {
            result_text = msgModel.recipient;
        }
        else if ([title isEqualToString:@"时限:"]) {
            result_text = [NSString stringWithFormat:@"%@分钟", msgModel.timeLimit];
        }
    }
    
    return result_text;
}


@end
